<template>
  <div id="belowTb" class="below-tb"></div>
</template>

<script>
import * as echarts from "echarts";

export default {
  props: {
    twodirt: {
      type: Array,
    },
  },
  // 质量图表
  methods: {
    // 质量图表
    drawLine() {
      if (
        this.myChart != null &&
        this.myChart != "" &&
        this.myChart != undefined
      ) {
        this.myChart.dispose(); //解决echarts dom已经加载的报错
      }
      this.myChart = echarts.init(document.getElementById("belowTb"));

      let option = {
        grid: {
          left: "13%",
          bottom: "40",
          // width: 600,
        },
        tooltip: {
          transitionDuration: 0,
          trigger: "axis",
          axisPointer: {
            type: "none",
          },
        },
        color: ["#0c7fbc"],
        xAxis: {
          type: "category",
          data: [
            "农业源(个)",
            "工业源(个)",
            "涉VOCs(个)",
            "扬尘工地(个)",
            "餐饮油烟(个)",
            "非道路移动源(个)",
          ],

          axisLabel: {
            color: "#a8deff",
            interval: 0,
            textStyle: {
              fontSize: 9,
            },
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: true,

            lineStyle: {
              color: "#283d73",
              width: 1,
              type: "solid",
            },
          },
        },
        yAxis: {
          type: "value",
          axisTick: {
            show: false,
          },

          axisLine: {
            show: true,
            lineStyle: {
              color: "#304c92",
              width: 1,
              type: "solid",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "#1c2a62",
              type: "solid",
            },
          },
        },
        series: [
          {
            barWidth: "38px",
            data: this.twodirt,
            type: "bar",
            itemStyle: {
              normal: {
                label: {
                  show: true,
                  position: "top",
                  color: "white",
                },
              },
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "#105aa1" }, //柱图渐变色

                { offset: 1, color: "#06b6e7" }, //柱图渐变色
              ]),
            },
          },
        ],
      };

      this.myChart.setOption(option);
    },

    // 窗口大小改变
    handleResize() {
      this.myChart.resize(); // 只要窗口大小改变 echarts跟着容器变化
    },
  },
  mounted() {
    // 监听窗口大小改变
    window.addEventListener("resize", this.handleResize);

    this.drawLine();
  },
  watch: {
    twodirt: {
      deep: true, // 开启深度监听
      handler(newValue, oldValue) {
        // 观察到数据变化 重新渲染
        this.myChart.clear(); // 清除画布
        this.drawLine(); // 重新绘制
      },
    },
  },
  // 销毁前
  beforeDestroy() {
    // 解绑事件 [性能优化]
    window.removeEventListener("resize", this.handleResize);
  },
};
</script>
<style lang="less" scoped>
.below-tb {
  height: 4.5rem;
}
</style>